package com.ylz.sjzx.sio.core;

import com.alipay.remoting.AsyncContext;
import com.alipay.remoting.BizContext;
import com.alipay.remoting.rpc.protocol.AsyncUserProcessor;
import com.ylz.sjzx.sio.common.core.core.IAdapter;
import com.ylz.sjzx.sio.common.core.core.SioBase;
import com.ylz.sjzx.sio.common.core.core.container.common.annotation.AdapterDesc;
import com.ylz.sjzx.sio.common.core.exception.PreParamException;
import com.ylz.sjzx.sio.core.group.model.CmdMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author
 * @version 1.0
 * @date 2021/2/18
 */
@Component
@Slf4j
public class CmdMessageProcessor extends AsyncUserProcessor<CmdMessage> {
    @Override
    public void handleRequest(BizContext bizContext, AsyncContext asyncContext, CmdMessage cmdMessage) {
        log.info("收到消息");
        SioBase.clean();
        IAdapter adapter = SioBase.getInnerAdapterMap().get(cmdMessage.getCmd());
        if (adapter == null) {
            log.error("adapter 为null ", cmdMessage);
            throw new PreParamException("入参异常");
        }
        //将接口的desc注解存入context
        SioBase.setIAdapterDesc(adapter.getClass().getAnnotation(AdapterDesc.class));
        //正常请求参数数组两
        SioBase.setParam(cmdMessage);
        adapter.invoke();
    }

    @Override
    public String interest() {
        return CmdMessage.class.getName();
    }
}
